<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars 
define("PRIMARY_KEY","CUCODE");
$PRIMARY_KEY    = PRIMARY_KEY;
$PRIMARY_VALUE  = GetParam($PRIMARY_KEY,"");
$FileName       = "ais_setup_ar.php";
$FileTemplate   = "ais_setup_ar_form.html";
$SubHeaderTitle = "Setup Account Receivable";
$TableName			= "ais_piutang";
$HTMLFormName   = "ARForm";
$MaxGrid        = "50";
//$HTMLFormName2  = "BudgetForm";

$FormAction    = $FileName . "?" . AddParam(GetQueryString("QueryString", ""),"form","$HTMLFormName");
$FormEncType   = "application/x-www-form-urlencoded";
//$PMode         = "";

//Initiate Database Table For Insert & Update;
$FormFields    = array("nobkt"=>"0","tglbkt"=>"date","uraian"=>"0","curr"=>"0","jumlah"=>"number","rate"=>"number","skada"=>"0","ktrasl"=>"");

//buildsqlfield ("$TableName", $FormFields);
$KTRASL = GetOfficeID();
//Leave Here Untouch
$FormCName     = BuildHTMLField($FormFields,"name",1);
$FormCValue    = BuildHTMLField($FormFields,"value",1);

//************ FORM ACTION **************
if (trim(GetParam("form",""))==trim($HTMLFormName)) {
	if (GetParam("FormAction","")=="Save") {
		FormSave($TableName, "nobkt", $FormFields, array("debtur"=>GetParam("CustomerCode",""),"normut"=>"999","debnam"=>GetParam("CustomerName",""),"ktrasl"=>GetOfficeID()));
		DeleteRecord ("txtCheck",$MaxGrid);
		header("Location: $FileName?PageMode=Form&".PRIMARY_KEY."=".$PRIMARY_VALUE);
		exit;
	}

	if (GetParam("FormAction","")=="Delete") {
//		print "Deleted"."<br>\n";
		DeleteRecord ("txtCheck",$MaxGrid);		
	}
}

//Field Verification 
//$Verification  = "False";

$SessionVar    = md5(microtime());
//$SessionVar    = "Stuck";
$VerifyFields = array("nobkt"=>"ID NUM#","tglbkt"=>"DATE","curr"=>"CURRENCY","jumlah"=>"AMOUNT","skada"=>"COA");
$SQLCount     = "SELECT count(*) FROM `$TableName`";
$SQLCount    .= "WHERE debtur='".$PRIMARY_VALUE."'";
$Count        = $DBConnection->dbc->get_var($SQLCount);
$NewDataStart = $Count+1;

$InitFunction = "Masking()";

Init($SessionVar,BuildMasking ($HTMLFormName,MutatorFields($FormFields,($MaxGrid+$Count))));

FormGridHTML ($HTMLFormName,$VerifyFields,$Count,$MaxGrid,$SessionVar);

//************ SET FORM MODE ***************
if (GetParam(PRIMARY_KEY,"")) $FormMode = "Edit";
else $FormMode = "Add";

//******* DATABASE FORM QUERY ***********
$SQL = "SELECT ".BuildFields($FormFields)." FROM `$TableName`";
if (GetParam(PRIMARY_KEY,"") && $FormMode="Edit") {
    $SQLForm  = $SQL."WHERE debtur ='".GetParam(PRIMARY_KEY,"")."'";
    $SQLForm .= " ORDER BY nobkt";
}

//Select Customer: HEADER FORM
$SQLCust = "SELECT * FROM $DBGeneral.tbldebitur WHERE CUCODE='".GetParam(PRIMARY_KEY,"")."'";

//Select Currency Table
$SQLCurr = "SELECT * FROM tblcurrency";


//Select GRID Result
$Result = $DBConnection->dbc->get_results($SQLForm,ARRAY_A);
$Result  = BuildEmptyGrid ($FormFields, $MaxGrid, $Result);

//print $SQLForm;
//******* SHOW FORM CONTENT *********
$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);

if (GetParam(PRIMARY_KEY,"")) {$TBS->MergeBlock("blv","mysql",$SQLForm);}
else $TBS->MergeBlock("blv",$FormCValue);
//HTML Name
$TBS->MergeBlock("bln",$FormCName);
//HTMLGrid

$TBS->MergeBlock("blcv",$DBMysql,$SQLCust);

$TBS->MergeBlock("currblk",$DBMysql,$SQLCurr);
$TBS->MergeBlock("blg",$Result);
$TBS->Show();


//*************** ACTION FUNCTION *******************
function FormInsert() {
	global $DBConnection,$FileName;
	$DBConnection->Insert();
	header("Location: $FileName?PageMode=Form");
	exit;
}

function FormUpdate() {
	global $DBConnection;
	$DBConnection->Update();
}

function FormDelete() {
	global $DBConnection,$FileName,$TableName;
	$DBConnection->deleteGrid("$TableName","nobkt");
	header("Location: $FileName");
	exit;	
}

function FormSearch () {

}

function FormCancel () {
	global $FileName;
	header("Location: ".$FileName);
	exit;
}

function FormSave ($table, $keyField, $FormFields, $SpecialCase="") {
	global $FileName, $MaxGrid, $DBConnection;
	$TotalData = $MaxGrid * 2;
	$NewDataStart = GetParam("NewData","0");
	for ($a=1;$a<=$TotalData;$a++) {
		foreach ($FormFields as $key=>$formatting) {
			if (!GetParam($keyField.$a,"")) {
				$EndOfData = 1;
				break;
			}
			$value = GetParam($key.$a,"");
			if ($formatting=="number") $value = str_replace(",","",$value);
			if ($formatting=="date") {
				list($day,$month,$year) = explode("/",$value);
				$value = "$year-$month-$day";
		    }
			
			$DBConnection->add_data($table,$key,$value);
		}
		if ($EndOfData) {
			break;
	    }

		if (strlen($SpecialCase)) {
			foreach ($SpecialCase as $key => $value) {
				$DBConnection->add_data($table,$key,$value);
			}
		}
		if ($a < $NewDataStart) {
			$DBConnection->UpdateGrid($keyField,GetParam($keyField.$a,""));			
		} else {
			$DBConnection->InsertGrid();

	    }
	}
}
//*************** USER FUNCTION *******************
function buildsqlfield ($table, $arrayfield) {
		global $DBConnection;
		for ($x=0;$x<count($arrayfield);$x++) {
			$DBConnection->add_htmlfield($table,$arrayfield[$x],GetParam($arrayfield[$x],""));
		}
}

function DeleteRecord ($sequence_name,$PageSize) {
	global $DBConnection,$FileName,$TableName;	
	for ($x=1;$x<=$PageSize;$x++) {				
		if (GetParam($sequence_name.$x,""))	{
			$DBConnection->deleteGrid("$TableName","nobkt",$sequence_name.$x);
	    }
	}
}

function BuildFields ($FormFields) {
		global $DBConnection;
		$str = "";
		foreach ($FormFields as $key=>$formatting) {
			if (strlen($str)) $str .= ",$key";
			else $str = "$key";
		}
		return $str;
}

function MutatorFields ($FormFields, $MaxGrid) {
		global $DBConnection;
		$array_fields = array();
		for ($x=0;$x<$MaxGrid;$x++) {
			foreach ($FormFields as $key=>$formatting){
				$a=$x+1;
				$array_fields[$key.$a] = "$formatting";
			}			
		}
		return $array_fields;
}
?>